package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车(day07任务三)
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {

        ShoppingCart queryShoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, queryShoppingCart);
        queryShoppingCart.setUserId(BaseContext.getCurrentId());

        log.info("[添加购物车] shoppingCart:{}", queryShoppingCart);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.selectByAll(queryShoppingCart);
        ShoppingCart dbShoppingCart = null;

        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            dbShoppingCart = shoppingCartList.get(0);
            dbShoppingCart.setNumber(dbShoppingCart.getNumber() + 1);

            shoppingCartMapper.updateShoppingCartById(dbShoppingCart);
        } else {
            Long dishId = queryShoppingCart.getDishId();
            if (dishId != null) {
                Dish dish = dishMapper.selectById(dishId);
                queryShoppingCart.setName(dish.getName());
                queryShoppingCart.setAmount(dish.getPrice());
                queryShoppingCart.setImage(dish.getImage());
            } else {
                Setmeal setmeal = setmealMapper.selectById(queryShoppingCart.getSetmealId());

                queryShoppingCart.setName(setmeal.getName());
                queryShoppingCart.setAmount(setmeal.getPrice());
                queryShoppingCart.setImage(setmeal.getImage());
            }
            queryShoppingCart.setNumber(1);
            queryShoppingCart.setCreateTime(LocalDateTime.now());
            log.info("[添加购物车记录]shoppingCart:{}", queryShoppingCart);
            shoppingCartMapper.insertShoppingCart(queryShoppingCart);
        }


    }

    /**
     * 查看购物车(day07任务四)
     *
     * @return
     */
    @Override
    public List<ShoppingCart> findCartList() {
        ShoppingCart queryShoppingCart = new ShoppingCart();
        queryShoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.selectByAll(queryShoppingCart);
    }

    /**
     * 清空购物车(day07任务五)
     */
    @Override
    public void cleanCart() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
